#include "Tools.h"
#include <string>
#include <Shlwapi.h>
#include <vector>

const wchar_t* to_wide( const std::string& strToConvert ) {
	
	const size_t newsize = 256;
 
	size_t origsize = strlen(strToConvert.c_str()) + 1;
	size_t convertedChars = 0;
	wchar_t * wcstring = new wchar_t[newsize];
 
	mbstowcs_s (&convertedChars, wcstring, origsize, strToConvert.c_str(), _TRUNCATE);

    return wcstring;
}

std::string GetDirectoryPath(std::string pFilePath)
{
	size_t s = strlen(pFilePath.c_str());
	char * buffer = new char[s];
	memcpy(buffer, pFilePath.c_str(), s + 1);
	PathRemoveFileSpecA(buffer);
	return std::string(buffer);
}

void GetVector3D(std::string pStrVector3d, XMFLOAT3 &vector)
{
	std::vector<std::string> spilttedString = split(pStrVector3d, ';');
	vector.x = (float)::atof(spilttedString[0].c_str());
	vector.y = (float)::atof(spilttedString[1].c_str());
	vector.z = (float)::atof(spilttedString[2].c_str());
}

XMFLOAT4 StringToQuaternion(std::string pStrYPR)
{
	std::vector<std::string> spilttedString = split(pStrYPR, ';');
	float y = (float)::atof(spilttedString[0].c_str());
	float p = (float)::atof(spilttedString[1].c_str());
	float r = (float)::atof(spilttedString[2].c_str());
	XMFLOAT4 result;
	XMStoreFloat4(&result, XMQuaternionRotationRollPitchYaw(p, y, r));
	return result;
}

std::vector<std::string> split(const std::string& s, char seperator)
{
   std::vector<std::string> output;

    std::string::size_type prev_pos = 0, pos = 0;

    while((pos = s.find(seperator, pos)) != std::string::npos)
    {
        std::string substring( s.substr(prev_pos, pos-prev_pos) );

        output.push_back(substring);

        prev_pos = ++pos;
    }

    output.push_back(s.substr(prev_pos, pos-prev_pos)); // Last word

    return output;
}

void AddBoundingBox(BoundingBox &pBoundingBox, BoundingBox &pBoundingBoxToAdd)
{
		pBoundingBox.x1 = min(pBoundingBox.x1, pBoundingBoxToAdd.x1);
		pBoundingBox.y1 = min(pBoundingBox.y1, pBoundingBoxToAdd.y1);
		pBoundingBox.z1 = min(pBoundingBox.z1, pBoundingBoxToAdd.z1);
		pBoundingBox.x2 = max(pBoundingBox.x2, pBoundingBoxToAdd.x2);
		pBoundingBox.y2 = max(pBoundingBox.y2, pBoundingBoxToAdd.y2);
		pBoundingBox.z2 = max(pBoundingBox.z2, pBoundingBoxToAdd.z2);
}
